package batch.import_user;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.*;

import java.util.ArrayList;
import java.sql.*;
import java.io.*;

import batch.common.db.ConnectionManager;

public class makeTempTableWithIdnPwnPostnAd
{
    private final String FILE_NAME  = "/home01/sfc/batch/import_user/data/gbr_200610_IPT_My_Offer_3.txt";
    private final String TABLE_NAME = "G_GBR_IMPORT_TEMP_2";

    //arrStrData[0] : email
    //arrStrData[1] : gender
    //arrStrData[2] : firstname
    //arrStrData[3] : lastname
    //arrStrData[4] : address
    //arrStrData[5] : postcode
    //arrStrData[6] : birthday
    //arrStrData[7] : hp_brand

    public static void main(String[] args)
    {
        Logger.getLogger("import_user.makeTempTableWithIdnPwnPostnAd").info("( * ) Start.");

        makeTempTableWithIdnPwnPostnAd obj = new makeTempTableWithIdnPwnPostnAd();
        obj.init();

        Logger.getLogger("import_user.makeTempTableWithIdnPwnPostnAd").info("( * ) End.");
    }


    public void init(){
        Connection conn = null;

        BufferedReader bfrdForFile = null;

        String strTemp   = "";
        String strUserId = "";
        String strPasswd = "";

        int iRE = 0;

        ArrayList params = new ArrayList();

        StringBuffer strQuery = new StringBuffer()
                               .append("INSERT INTO  " + TABLE_NAME + "( SEQ       \n")
                               .append("                               , USERID    \n")
                               .append("                               , PASSWD    \n")
                               .append("                               , FNAME     \n")
                               .append("                               , LNAME     \n")
                               .append("                               , GENDER    \n")
                               .append("                               , EMAIL     \n")
                               .append("                               , BIRTHDAY  \n")
                               .append("                               , ADDRESS   \n")
                               .append("                               , POSTCODE  \n")
                               .append("                               , HP_BRAND) \n")
                               .append("                         VALUES( ?         \n")
                               .append("                               , ?         \n")
                               .append("                               , ?         \n")
                               .append("                               , ?         \n")
                               .append("                               , ?         \n")
                               .append("                               , ?         \n")
                               .append("                               , ?         \n")
                               .append("                               , ?         \n")
                               .append("                               , ?         \n")
                               .append("                               , ?         \n")
                               .append("                               , ?)        \n");

        Logger.getLogger("import_user.makeTempTableWithIdnPwnPostnAd").info(FILE_NAME);

        try{
            bfrdForFile = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME), "ISO-8859-1"));

            checkManager chkMgr = new checkManager();

            conn = ConnectionManager.getConnection();
            conn.setAutoCommit(true);

            QueryRunner qRunner = new QueryRunner();

            for(int i=0; (strTemp = bfrdForFile.readLine()) != null; i++) {
                String arrStrData[] = StringUtils.splitPreserveAllTokens(strTemp, "\t");

                for(int l=0;l<arrStrData.length;l++){
                    arrStrData[l] = StringUtils.stripToEmpty(arrStrData[l]);
                }

                //Rule : LastName (if duplicate : LastName + Last Count Number)
                strUserId = chkMgr.removeCharacters(arrStrData[3].toLowerCase());
                strUserId = chkMgr.getNewUserId(conn, strUserId, TABLE_NAME);

                //Rule : pw + (Birthday - DDMMYYYY)
                if(arrStrData[6].length() == 8 && NumberUtils.isDigits(arrStrData[6])){
                    strPasswd = "pw" + arrStrData[6].substring(6, 8)
                                     + arrStrData[6].substring(4, 6)
                                     + arrStrData[6].substring(0, 4);
                }
                else{
                    strPasswd = "pw";
                }

                params.clear();
                params.add(new Long(i + 1)                                             );  //SEQ
                params.add(strUserId                                                   );  //USERID
                params.add(strPasswd                                                   );  //PASSWD
                params.add(StringUtils.defaultIfEmpty(arrStrData[2], ".")              );  //FNAME
                params.add(StringUtils.defaultIfEmpty(arrStrData[3], ".")              );  //LNAME
                params.add(StringUtils.defaultIfEmpty(arrStrData[1], "M").toUpperCase());  //GENDER
                params.add(arrStrData[0]                                               );  //EMAIL
                params.add(arrStrData[6]                                               );  //BIRTHDAY
                params.add(arrStrData[4]                                               );  //ADDRESS
                params.add(arrStrData[5]                                               );  //POSTCODE
                params.add(arrStrData[7]                                               );  //HP_BRAND

                try{
                    iRE = qRunner.update(conn, strQuery.toString(), params.toArray());
                }
                catch(Exception ex){
                    ex.printStackTrace();
                    Logger.getLogger("import_user.makeTempTableWithIdnPwnPostnAd").info(ex.toString());
                }

                if(i % 500 == 0){
                    Logger.getLogger("import_user.makeTempTableWithIdnPwnPostnAd").info("here we are == " + i);
                }
            }
        }
        catch(Exception ex) {
            ex.printStackTrace();
            Logger.getLogger("import_user.makeTempTableWithIdnPwnPostnAd").info(ex.toString());
        }
        finally {
            if (bfrdForFile != null) try { bfrdForFile.close(); } catch (Exception se) {}
            if (conn != null) try { conn.close(); } catch (Exception se) {}
        }
    }
}
